package com.ossean.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.model.EddProjects;
import com.ossean.model.EddRelations;
import com.ossean.model.GatherProjectsModel;
import com.ossean.model.OpenSourceProject;
import com.ossean.model.Synonymmings;
import com.ossean.model.Synonyms;
import com.ossean.model.Taggings;
import com.ossean.model.Tags;
import com.ossean.model.UpdateControlProjects;

public interface DBSource {
	
   //读取gather_projects表中的数据 根据id in来读取
	@Select("select * from ${table} where id=#{id}")
	public GatherProjectsModel getGatherProjectById(@Param("table") String table, @Param("id") int id);
	//查看指针edd_pointers表，查看处理数量
	@Select("select Pointer from ${table} where SourceTableName=#{SourceTableName} and TargetTableName=#{TargetTableName}")
	public int getPointer(@Param("table") String table, @Param("SourceTableName") String SourceTableName, @Param("TargetTableName") String TargetTableName);
	
	//读取表中最小id值
	@Select("select min(id) from ${table}")
	public int getMinId(@Param("table") String table);
	
	//读取表中最大id值
	@Select("select max(id) from ${table}")
	public int getMaxId(@Param("table") String table);
	
	//根据标签id读取Tag对象
	@Select("select * from ${table} where id=#{id}")
	public Tags getTagById(@Param("table") String table, @Param("id") int id);
	
	//从taggings表中读取对应gather_projects_id的数据
	@Select("select * from ${table} where taggable_id=#{id} and taggable_type='OpenSourceProject'")
	public List<Taggings> getTaggingsListByTaggableId(@Param("table") String table, @Param("id") int id);
	
	//删除taggings表中对应tagging_id和taggable_type的数据
	@Delete("delete from ${table} where taggable_id=#{id} and taggable_type='OpenSourceProject'")
	public void deleteTaggingsByTaggableId(@Param("table") String table, @Param("id") int id);
	
	//读取tagid和taggable_type对应的数据条目数
	@Select("select count(*) from ${table} where (tag_id=#{tagId1} or tag_id=#{tagId2}) and taggable_type='OpenSourceProject'")
	public int getNumOfTagIdAndOspByTwoTagIds(@Param("table") String table, @Param("tagId1") int tagId1, @Param("tagId2") int tagId2);
	
	//重载的上面一个函数
	@Select("select count(*) from ${table} where tag_id=#{tagId1} and taggable_type='OpenSourceProject'")
	public int getNumOfTagIdAndOsp(@Param("table") String table, @Param("tagId1") int tagId);
	
	
	//对新的EddProject对象进行更新
	@Update("update ${table} set "
			+ "name=#{project.name},description=#{project.description},followers_num=#{project.followers_num},"
			+ "url=#{project.url},language=#{project.language},download_num=#{project.download_num},view_num_crawled=#{project.view_num_crawled},"
			+ "category=#{project.category},crawled_time=#{project.crawled_time},source=#{project.source},view_num_local=#{project.view_num_local},"
			+ "created_at=#{project.created_at},updated_at=#{project.updated_at},composite_score=#{project.composite_score},"
			+ "relative_memos_num=#{project.relative_memos_num},created_time=#{project.created_time},updated_time=#{project.updated_time},"
			+ "view_num=#{project.view_num},activeness=#{project.activeness},tags=#{project.tags},name_value=#{project.name_value} where id=#{project.id}")
	public void updateEddProject(@Param("table") String table, @Param("project") EddProjects project);
	
	@Insert("insert into ${table} "
			+ "(name,description,followers_num,url,language,download_num,view_num_crawled,category,crawled_time,source,view_num_local,created_at,"
			+ "updated_at,composite_score,relative_memos_num,created_time,updated_time,view_num,activeness,tags,name_value) "
			+ "values "
			+ "(#{project.name},#{project.description},#{project.followers_num},#{project.url},#{project.language},"
			+ "#{project.download_num},#{project.view_num_crawled},#{project.category},#{project.crawled_time},#{project.source},#{project.view_num_local},"
			+ "#{project.created_at},#{project.updated_at},#{project.composite_score},#{project.relative_memos_num},#{project.created_time},#{project.updated_time},"
			+ "#{project.view_num},#{project.activeness},#{project.tags},#{project.name_value})")
	public void insertEddProject(@Param("table") String table, @Param("project") EddProjects project);
	
	@Insert("update ${table} set homepage=#{homepage} where id =#{id}")
	public void updateApacheHomepageById(@Param("table") String table, @Param("homepage") String homepage, @Param("id") int id);
	
	@Insert("insert into ${table} (`SourceTableName`,`TargetTableName`,`Pointer`) values (#{SourceTableName},#{TargetTableName},#{Pointer})")
	public void insertPointer(@Param("table") String table, @Param("SourceTableName") String SourceTableName, @Param("TargetTableName") String TargetTableName, @Param("Pointer") int Pointer);

	@Update("update ${table} set Pointer=#{Pointer} where SourceTableName=#{SourceTableName} and TargetTableName=#{TargetTableName}")
	public void updatePointer(@Param("table") String table, @Param("SourceTableName") String SourceTableName, @Param("TargetTableName") String TargetTableName, @Param("Pointer") int Pointer);

	//插入Taggings对象
	@Insert("insert ignore into ${table} (tag_id,urlMD5,taggable_type,context,created_at,disagree_num) values (#{model.tag_id},#{model.urlMD5},#{model.taggable_type},#{model.context},#{model.created_at},#{model.disagree_num})")
	public void insertTaggings(@Param("table") String table, @Param("model") Taggings model);
	
	//读取同义词表中得所有同义词
	@Select("select name from ${synonyms} where id in (select synonym_id from ${synonymmings} where synonymming_id=#{synonymming_id})")
	public List<String> getSynonyms(@Param("synonyms") String synonyms, @Param("synonymmings") String synonymmings, @Param("synonymming_id") int synonymming_id);
	
	//插入别名到synonym表
	@Insert("insert ignore into ${table} (name) values (#{model.name})")
	public void insertSynonym(@Param("table") String table, @Param("model") Synonyms model);
	
	//插入别名到synonym表
	@Insert("insert ignore into ${table} (prjId,synonyms,flag) VALUES (#{prjId},#{synonyms},#{flag})")
	public void insertSynonymsNew(@Param("table") String table, @Param("prjId") int prjId,@Param("synonyms") String synonyms,@Param("flag") int flag);
	
	//根据同义词名查找同义词对象
	@Select("select * from ${table} where name=#{name}")
	public Synonyms getSynonymByName(@Param("table") String table, @Param("name") String name);
	
	@Update("update ${table} set flag=#{flag} where prjId=#{id}")
	public void updateSynonyms(@Param("table") String table, @Param("id") int id,@Param("flag") int flag);
	
	//插入Synonymmings对象
	@Insert("insert ignore into ${table} (synonym_id,synonymming_id,flag) values (#{model.synonym_id},#{model.synonymming_id},#{model.flag})")
	public void insertSynonymming(@Param("table") String table, @Param("model") Synonymmings model);
	
	//取出synonymmings对象
	@Select("select * from ${table} where synonym_id=#{synonym_id} and synonymming_id=#{synonymming_id}")
	public Synonymmings selectSynonymming(@Param("table") String table, @Param("synonym_id") int synonym_id, @Param("synonymming_id") int synonymming_id);
	
	//根据synonym_id找到所有synonymmings对象
	@Select("select * from ${table} where synonym_id=#{synonym_id}")
	public List<Synonymmings> selectSynonymmingListBySynonymId(@Param("table") String table, @Param("synonym_id") int synonym_id);
	
	//读取synonymmings表
	@Select("select synonym_id from ${table} where synonymming_id=#{synonymming_id} and flag=1")
	public List<Integer> getSynonymmingList(@Param("table") String table, @Param("synonymming_id") int synonymming_id);
	
	//读取synonym对象
	@Select("select * from ${table} where id=#{id}")
	public Synonyms getSynonymById(@Param("table") String table, @Param("id") int id);
	
	//读取对应于项目ID的synonym对象
	@Select("select synonyms from ${table} where prjId=#{id} and flag=0;")
	public List<String> getSynonymByPrjId(@Param("table") String table, @Param("id") int id);
	
	//transfer中:读取对应于项目ID的synonym对象，只需确定的flag=1别名 
	@Select("select synonyms from ${table} where prjId=#{id} and flag=1;")
	public List<String> getSynonymByPrjIdForTransfer(@Param("table") String table, @Param("id") int id);

	//读取对应于项目ID的synonym对象
	@Select("select prjId from ${table} where synonyms=#{synonyms}")
	public List<Integer> getSynonymBySynonyms(@Param("table") String table, @Param("synonyms") String synonyms);
	
	//读取synonym对象
	@Select("select name from ${table} where id=#{id}")
	public String getSynonymNameById(@Param("table") String table, @Param("id") int id);

	//根据url检测是否存在匹配结果
	@Select("select * from ${table} where url like '%|:|${url}' escape '/' or url like '%|:|${url}|,|%' escape '/'")
	public EddProjects selectEddProjectsByUrl(@Param("table") String table, @Param("url") String url);
	
	@Select("select * from ${table} where id=#{id}")
	public EddProjects selectEddProjectsById(@Param("table") String table, @Param("id") int id);
	
	//获取edd_relations表中记录相应gather_projects_id的记录
	@Select("select * from ${table} where gather_projects_ids like '%,${gather_projects_id},%'")
	public EddRelations getEddRelationsByGatherProjectsId(@Param("table") String table, @Param("gather_projects_id") int gather_projects_id);
	
	
	
	//删除edd_relations表中对应id的记录
	@Delete("delete from ${table} where id=#{id}")
	public void deleteEddRelationsItem(@Param("table") String table, @Param("id") int id);
	
	//向edd_relations表插入记录
	@Insert("insert into ${table} (gather_projects_ids,flag) values (#{gather_projects_ids},#{flag})")
	public void insertEddRelations(@Param("table") String table, @Param("gather_projects_ids") String gather_projects_ids,
			@Param("flag") int flag);

	
	
	
}